<template>
  <div class="retail-dashboard">
    <div class="dashboard-header">
      <h1>零售管理系统</h1>
      <p>专业的零售业务管理平台</p>
    </div>

    <div class="dashboard-grid">
      <!-- 零售出库 -->
      <div class="dashboard-card" @click="navigateTo('/retail/outbound')">
        <div class="card-icon">
          <i class="fas fa-shopping-cart"></i>
        </div>
        <div class="card-content">
          <h3>零售出库</h3>
          <p>VIP会员和散户零售单据录入</p>
          <div class="card-stats">
            <span class="stat-item">
              <strong>今日出库</strong>
              <span class="stat-value">{{ todayOutbound }}</span>
            </span>
            <span class="stat-item">
              <strong>本月出库</strong>
              <span class="stat-value">{{ monthOutbound }}</span>
            </span>
          </div>
        </div>
      </div>

      <!-- 零售退货 -->
      <div class="dashboard-card" @click="navigateTo('/retail/returns')">
        <div class="card-icon">
          <i class="fas fa-undo"></i>
        </div>
        <div class="card-content">
          <h3>零售退货</h3>
          <p>关联原出库单，自动调整库存与资金</p>
          <div class="card-stats">
            <span class="stat-item">
              <strong>待处理</strong>
              <span class="stat-value">{{ pendingReturns }}</span>
            </span>
            <span class="stat-item">
              <strong>本月退货</strong>
              <span class="stat-value">{{ monthReturns }}</span>
            </span>
          </div>
        </div>
      </div>

      <!-- 订单跟踪 -->
      <div class="dashboard-card" @click="navigateTo('/retail/tracking')">
        <div class="card-icon">
          <i class="fas fa-truck"></i>
        </div>
        <div class="card-content">
          <h3>订单跟踪</h3>
          <p>实时跟踪订单状态：下单、出库、配送</p>
          <div class="card-stats">
            <span class="stat-item">
              <strong>进行中</strong>
              <span class="stat-value">{{ activeOrders }}</span>
            </span>
            <span class="stat-item">
              <strong>今日配送</strong>
              <span class="stat-value">{{ todayDelivery }}</span>
            </span>
          </div>
        </div>
      </div>

      <!-- 商品管理 -->
      <div class="dashboard-card" @click="navigateTo('/retail/products')">
        <div class="card-icon">
          <i class="fas fa-boxes"></i>
        </div>
        <div class="card-content">
          <h3>商品管理</h3>
          <p>对商品进行一系列操作管理</p>
          <div class="card-stats">
            <span class="stat-item">
              <strong>商品总数</strong>
              <span class="stat-value">{{ totalProducts }}</span>
            </span>
            <span class="stat-item">
              <strong>低库存</strong>
              <span class="stat-value">{{ lowStock }}</span>
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 快速操作 -->
    <div class="quick-actions">
      <h3>快速操作</h3>
      <div class="action-buttons">
        <button class="action-btn primary" @click="navigateTo('/retail/outbound/create')">
          <i class="fas fa-plus"></i>
          新建零售出库单
        </button>
        <button class="action-btn secondary" @click="navigateTo('/retail/returns/create')">
          <i class="fas fa-undo"></i>
          新建退货单
        </button>
        <button class="action-btn tertiary" @click="navigateTo('/retail/products/create')">
          <i class="fas fa-box"></i>
          添加商品
        </button>
      </div>
    </div>

    <!-- 最近活动 -->
    <div class="recent-activities">
      <h3>最近活动</h3>
      <div class="activity-list">
        <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
          <div class="activity-icon" :class="activity.type">
            <i :class="activity.icon"></i>
          </div>
          <div class="activity-content">
            <p class="activity-title">{{ activity.title }}</p>
            <p class="activity-time">{{ activity.time }}</p>
          </div>
          <div class="activity-status" :class="activity.status">
            {{ activity.statusText }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'RetailIndex',
  setup() {
    const router = useRouter()
    
    // 统计数据
    const todayOutbound = ref(0)
    const monthOutbound = ref(0)
    const pendingReturns = ref(0)
    const monthReturns = ref(0)
    const activeOrders = ref(0)
    const todayDelivery = ref(0)
    const totalProducts = ref(0)
    const lowStock = ref(0)
    
    // 最近活动
    const recentActivities = ref([
      {
        id: 1,
        type: 'outbound',
        icon: 'fas fa-shopping-cart',
        title: 'VIP客户张三完成iPhone 13购买',
        time: '2分钟前',
        status: 'success',
        statusText: '已完成'
      },
      {
        id: 2,
        type: 'return',
        icon: 'fas fa-undo',
        title: '客户李四申请MacBook Pro退货',
        time: '15分钟前',
        status: 'pending',
        statusText: '待处理'
      },
      {
        id: 3,
        type: 'delivery',
        icon: 'fas fa-truck',
        title: '订单RSO202409001已发货',
        time: '1小时前',
        status: 'shipped',
        statusText: '配送中'
      },
      {
        id: 4,
        type: 'product',
        icon: 'fas fa-box',
        title: '新增商品：Galaxy S22',
        time: '2小时前',
        status: 'success',
        statusText: '已添加'
      }
    ])

    // 导航函数
    const navigateTo = (path) => {
      router.push(path)
    }

    // 加载统计数据
    const loadStatistics = async () => {
      try {
        // 这里应该调用后端API获取统计数据
        // const response = await fetch('/api/retail/statistics')
        // const data = await response.json()
        
        // 模拟数据
        todayOutbound.value = 12
        monthOutbound.value = 156
        pendingReturns.value = 3
        monthReturns.value = 8
        activeOrders.value = 25
        todayDelivery.value = 18
        totalProducts.value = 45
        lowStock.value = 5
      } catch (error) {
        console.error('加载统计数据失败:', error)
      }
    }

    onMounted(() => {
      loadStatistics()
    })

    return {
      todayOutbound,
      monthOutbound,
      pendingReturns,
      monthReturns,
      activeOrders,
      todayDelivery,
      totalProducts,
      lowStock,
      recentActivities,
      navigateTo
    }
  }
}
</script>

<style scoped>
.retail-dashboard {
  padding: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

.dashboard-header {
  text-align: center;
  margin-bottom: 32px;
  color: white;
}

.dashboard-header h1 {
  font-size: 2.5rem;
  margin-bottom: 8px;
  font-weight: 700;
}

.dashboard-header p {
  font-size: 1.1rem;
  opacity: 0.9;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.dashboard-card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.dashboard-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.card-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  font-size: 24px;
  color: white;
}

.dashboard-card:nth-child(1) .card-icon {
  background: linear-gradient(135deg, #667eea, #764ba2);
}

.dashboard-card:nth-child(2) .card-icon {
  background: linear-gradient(135deg, #f093fb, #f5576c);
}

.dashboard-card:nth-child(3) .card-icon {
  background: linear-gradient(135deg, #4facfe, #00f2fe);
}

.dashboard-card:nth-child(4) .card-icon {
  background: linear-gradient(135deg, #43e97b, #38f9d7);
}

.card-content h3 {
  font-size: 1.5rem;
  margin-bottom: 8px;
  color: #2d3748;
  font-weight: 600;
}

.card-content p {
  color: #718096;
  margin-bottom: 16px;
  line-height: 1.5;
}

.card-stats {
  display: flex;
  gap: 16px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-item strong {
  font-size: 0.875rem;
  color: #718096;
  font-weight: 500;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: #2d3748;
}

.quick-actions {
  background: white;
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 32px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.quick-actions h3 {
  margin-bottom: 16px;
  color: #2d3748;
  font-weight: 600;
}

.action-buttons {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  border-radius: 8px;
  border: none;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
}

.action-btn.secondary {
  background: linear-gradient(135deg, #f093fb, #f5576c);
  color: white;
}

.action-btn.tertiary {
  background: linear-gradient(135deg, #43e97b, #38f9d7);
  color: white;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.recent-activities {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.recent-activities h3 {
  margin-bottom: 16px;
  color: #2d3748;
  font-weight: 600;
}

.activity-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.activity-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border-radius: 8px;
  background: #f7fafc;
  transition: all 0.3s ease;
}

.activity-item:hover {
  background: #edf2f7;
}

.activity-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
}

.activity-icon.outbound {
  background: linear-gradient(135deg, #667eea, #764ba2);
}

.activity-icon.return {
  background: linear-gradient(135deg, #f093fb, #f5576c);
}

.activity-icon.delivery {
  background: linear-gradient(135deg, #4facfe, #00f2fe);
}

.activity-icon.product {
  background: linear-gradient(135deg, #43e97b, #38f9d7);
}

.activity-content {
  flex: 1;
}

.activity-title {
  font-weight: 500;
  color: #2d3748;
  margin-bottom: 4px;
}

.activity-time {
  font-size: 0.875rem;
  color: #718096;
}

.activity-status {
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 0.875rem;
  font-weight: 500;
}

.activity-status.success {
  background: #c6f6d5;
  color: #22543d;
}

.activity-status.pending {
  background: #fef5e7;
  color: #744210;
}

.activity-status.shipped {
  background: #bee3f8;
  color: #1a365d;
}

@media (max-width: 768px) {
  .dashboard-grid {
    grid-template-columns: 1fr;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .card-stats {
    flex-direction: column;
    gap: 8px;
  }
}
</style>
